package android.slc.appbase.vm;

import android.app.Application;
import android.slc.commonlibrary.util.ViewModelProviderFactory;
import android.slc.extras.component.dict.vmbox.TreeDictVmBox;
import android.slc.extras.component.location.domain.LocationVmBox;
import android.slc.extras.system.domain.SystemVmBox;
import android.slc.extras.user.domain.UserVmBox;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;

import com.blankj.utilcode.util.CacheMemoryUtils;

/**
 * 全局数据
 *
 * @author slc
 * @date 2020-08-18 17:37
 */
public class GlobalDataVm extends AndroidViewModel {
    private final CacheMemoryUtils baseCacheMemoryUtils = CacheMemoryUtils.getInstance("baseCache", 512);
    public final SystemVmBox systemVmBox = new SystemVmBox();
    public final UserVmBox userVmBox = new UserVmBox();
    public final LocationVmBox locationVmBox = new LocationVmBox();
    public final TreeDictVmBox treeDictVmBox = new TreeDictVmBox();

    public static GlobalDataVm getInstance() {
        return ViewModelProviderFactory.getAppViewModelProvider().get(GlobalDataVm.class);
    }

    public GlobalDataVm(@NonNull Application application) {
        super(application);
    }

    public <T> T getData(String key) {
        return baseCacheMemoryUtils.get(key);
    }

    public void putData(String key, Object value) {
        baseCacheMemoryUtils.put(key, value);
    }

    public void clearAllData() {
        baseCacheMemoryUtils.clear();
        systemVmBox.clear();
        userVmBox.clear();
        locationVmBox.clear();
        treeDictVmBox.clear();
    }

}
